Page({
  data: {
    username: '',
    password: '',
    first_name: '',
    last_name: '',
    email: '',
    age: '',
    gender: '',
    errorMessage: ''
  },
  handleUsernameInput(e) {
    this.setData({
      username: e.detail.value
    });
  },
  handlePasswordInput(e) {
    this.setData({
      password: e.detail.value
    });
  },
  handleFirstNameInput(e) {
    this.setData({
      first_name: e.detail.value
    });
  },
  handleLastNameInput(e) {
    this.setData({
      last_name: e.detail.value
    });
  },
  handleEmailInput(e) {
    this.setData({
      email: e.detail.value
    });
  },
  handleAgeInput(e) {
    this.setData({
      age: e.detail.value
    });
  },
  handleGenderInput(e) {
    this.setData({
      gender: e.detail.value
    });
  },
  handleRegisterSubmit() {
    const { username, password, first_name, last_name, email, age, gender } = this.data;
    const requiredFields = {
      username,
      password,
      first_name,
      last_name,
      email,
      gender
    };
    let hasEmptyField = false;
    let errorMsg = '';
    for (const field in requiredFields) {
      if (!requiredFields[field]) {
        hasEmptyField = true;
        errorMsg = `${field} 不能为空`;
        break;
      }
    }
    if (hasEmptyField) {
      this.setData({
        errorMessage: errorMsg
      });
      return;
    }
    // 这里可以添加将用户数据发送到后端进行注册的逻辑，目前仅模拟成功提示
    wx.showToast({
      title: '注册成功',
      icon:'success'
    });
    wx.navigateBack({
      delta: 1
    });
  }
});